<template>
  <div id="app">
    <h1>购物车</h1>
    <ul>
      <li v-for="(item, index) in items" :key="index">
        {{ item.name }} - ¥{{ item.price }} x {{ item.quantity }}
        <button @click="decrementQuantity(item.name)">-</button>
        <button @click="incrementQuantity(item.name)">+</button>
      </li>
    </ul>
    <div>总计: ¥{{ total }}</div>
  </div>
</template>
<script>
import { mapState, mapActions, mapGetters } from 'vuex';

export default {
  computed: {
    ...mapState(['items']),
    ...mapGetters(['total'])
  },
  methods: {
    ...mapActions(['addItem', 'removeItem', 'incrementQuantity', 'decrementQuantity'])
  }
}
</script>
<style scoped>
div {
  text-align: center;
  margin: auto;
  width: 50%;
  padding: 20px;
  box-shadow: 0 4px 8px 0 rgba(0,0,0,0.2);
  transition: 0.3s;
  border-radius: 5px;
}

h1 {
  color: #333;
}

ul {
  list-style-type: none;
  padding: 0;
}

li {
  padding: 8px;
  margin: 5px 0;
  background-color: #f9f9f9;
  border: 1px solid #ddd;
  border-radius: 5px;
}

button {
  margin: 0 5px;
  color: white;
  border: none;
  cursor: pointer;
  padding: 10px 20px;
  border-radius: 5px;
  background-color: #555;
  transition: background-color 0.3s ease;
}

button:hover {
  background-color: #777;
}

.total {
  margin-top: 20px;
  font-size: 1.2em;
  font-weight: bold;
}
</style>

